<template>
  <div class="container">
    <PropTable
      :columns="column"
      :data="list"
      :total="total"
      @reset="reset"
      @onSubmit="onSubmit"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      @exportExcelAction="exportExcelAction"
      permissionPrefix="system:task"
    >
      <template v-slot:taskStatus="scope">
        {{ taskStatusPro(scope.row.taskStatus) }}
      </template>
      <template v-slot:auditStatus="scope">
        {{ auditStatusPro(scope.row.auditStatus) }}
      </template>
      <template v-slot:delayOrNot="scope">
        {{ isPostponeFn(scope.row.delayOrNot) }}
      </template>
      <template #btn>
        <div style="display: flex; justify-content: flex-end">
          <el-button type="primary" @click="handleAdd" 
          v-if="userStore.rolescode!=15"
            ><el-icon><plus /></el-icon>新建任务</el-button
          >
        </div>
      </template>

      <template v-slot:operation="scope">
        <div style="display: flex; justify-content: center">
          <el-button
            type="text"
            icon="Edit"
            @click="taskUpdata(scope.row)"
            v-if="
              userStore.nickName === scope.row.createPerson &&
              (scope.row.auditStatus === '0' || scope.row.auditStatus === '7') &&
              userStore.rolescode!=15
            "
          >
            任务编辑
          </el-button>
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleDetails(scope.row)"
            v-if="
              scope.row.auditStatus === '1' ||
              scope.row.auditStatus === '4' ||
              scope.row.auditStatus === '2'
            "
          >
            任务详情
          </el-button>

          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handeleRevoke(scope.row)"
            v-if="
              (scope.row.auditStatus === '2' || scope.row.auditStatus === '5')
              && userStore.rolescode!=15
            "
          >
            撤回
          </el-button>

          <el-button
            type="text"
            icon="Edit"
            v-if="
              (userStore.nickName === scope.row.createPerson &&
              scope.row.auditStatus === '1')
              && userStore.rolescode!=15
            "
            @click="achievement(scope.row)"
            >成果交付
          </el-button>
          <el-button
            v-if="
              userStore.nickName === scope.row.createPerson &&
              (scope.row.auditStatus === '6' || scope.row.auditStatus === '8')
              && userStore.rolescode!=15
            "
            type="text"
            icon="Edit"
            @click="deliverablesEdit(scope.row)"
            >成果交付编辑
          </el-button>
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleDelivery(scope.row)"
            v-if="scope.row.auditStatus === '4' && userStore.rolescode!=15"
          >
            成果交付详情
          </el-button>
          <!-- {{ userStore.userId }} -->

          <el-button
            type="text"
            icon="Delete"
            @click="delTask(scope.row)"
            v-if="
              userStore.userId === 1 &&
              ['0', '1','2', '4', '5', '6', '7', '8'].includes(
                scope.row.auditStatus
              )
              && userStore.rolescode!=15
            "
            >删除
          </el-button>
        </div>
      </template>
    </PropTable>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import PropTable from "@/components/Table/PropTable/index.vue";
import {
  getTaskList,
  delTaskApi,
  withdrawApi,
} from "@/api/taskManagement/task";
import { ElMessageBox, ElMessage } from "element-plus";
import { formatDate } from "@/utils/index";
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();
const router = useRouter();
const loading = ref(true);
const userStore = useUserStore();
// 任务状态
const taskStatusList = [
  {
    value: "0",
    label: "执行中",
  },
  {
    value: "2",
    label: "执行中",
  },
  {
    value: "5",
    label: "执行中",
  },
  {
    value: "6",
    label: "执行中",
  },
  {
    value: "1",
    label: "通过",
  },
  {
    value: "4",
    label: "已完成",
  },
];
// 审核状态
let auditStatusList = [
  {
    value: "2",
    label: "任务交付未审核",
  },
  {
    value: "0",
    label: "任务交付审核驳回",
  },
  {
    value: "1",
    label: "任务交付审核通过",
  },
  {
    value: "4",
    label: "成果交付审核通过",
  },
  {
    value: "5",
    label: "成果交付待审核",
  },
  {
    value: "6",
    label: "成果交付驳回",
  },
  {
    value: "7",
    label: "任务撤回",
  },
  {
    value: "8",
    label: "成果撤回",
  },
];
// 是否延期
const isPostpone = [
  {
    value: 0,
    label: "是",
  },
  {
    value: 1,
    label: "否",
  },
];
const column = [
  {
    name: "projectName",
    label: "项目名称",
    inSearch: true,
    valueType: "input",
  },
  // {
  //   name: "projectId",
  //   label: "项目编号",
  // },
  {
    name: "taskName",
    label: "任务名称",
    inSearch: true,
    valueType: "input",
  },
  {
    name: "taskStatus",
    label: "任务状态",
    // inSearch: true,
    valueType: "select",
    slot: true,
    options: taskStatusList,
  },
  {
    name: "auditStatus",
    label: "审核状态",
    inSearch: true,
    valueType: "select",
    slot: true,
    options: auditStatusList,
  },
  {
    name: "delayOrNot",
    label: "是否延期",
    slot: true,
  },
  {
    name: "taskCreateTime",
    label: "创建时间",
    inSearch: true,
    valueType: "datetime",
  },
  {
    name: "createPerson",
    label: "创建人",
  },
  { name: "operation", slot: true, fixed: "right", width: 300, label: "操作" },
];
const list = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
let paramsSubmit;
// 任务列表
const getTaskListFn = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  };
  if (paramsSubmit) {
    for (let key in paramsSubmit) {
      if (paramsSubmit[key]) {
        params[key] = paramsSubmit[key];
      }
    }
  }
  const res = await getTaskList(params);
  console.log(res);
  if (res.code === 200 && res.rows.length >= 0) {
    list.value = res.rows;
    total.value = res.total;
  } else {
    loading.value = false;
  }
};
// 导出
const exportExcelAction = (val) => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  };
  if (val) {
    for (let key in val) {
      if (val[key]) {
        params[key] = val[key];
      }
    }
  }
  proxy.download(
    "/system/task/export",
    params,
    `任务管理_${
      new Date().getFullYear() +
      "-" +
      new Date().getMonth() +
      1 +
      "-" +
      new Date().getDate()
    }.xlsx`
  );
};
// 任务状态处理
const taskStatusPro = (row) => {
  const taskPro = taskStatusList.find((item) => item.value === row);
  return taskPro ? taskPro.label : "";
};
// 处理审核状态
const auditStatusPro = (row) => {
  const auditPro = auditStatusList.find((item) => item.value === row);
  return auditPro ? auditPro.label : "";
};
// 是否延期
const isPostponeFn = (row) => {
  const isPostponeT = isPostpone.find((item) => item.value === row);
  return isPostponeT ? isPostponeT.label : "";
};
// 新增任务
const handleAdd = () => {
  router.push({
    path: "/taskManage/manage-add/add",
    query: { name: "1" },
  });
};

// 任务详情
const handleDetails = (row) => {
  router.push({
    path: "/taskManage/manage-add/add",
    query: { taskId: row.taskId, id: row.projectId, name: "3", task: "1" },
  });
};

// 成果交付详情
const handleDelivery = (row) => {
  router.push({
    path: "/taskManage/manage-add/add",
    query: { taskId: row.taskId, id: row.projectId, name: "3", task: "3" },
  });
};

//成果交付编辑
const deliverablesEdit = (row) => {
  router.push({
    path: "/taskManage/manage-add/add",
    query: { taskId: row.taskId, name: 2, id: row.projectId, task: "2" },
  });
};

// 成果交付新增
const achievement = (row) => {
  router.push({
    path: "/taskManage/manage-add/add",
    query: { taskId: row.taskId, name: "3", id: row.projectId, task: "2",nametype:"成果交付新增" },
  });
};
// 成果交付
// const deliverables = (row) => {
//   router.push({
//     path: "/taskManage/manage-add/add",
//     query: { taskId: row.taskId, id: row.projectId, name: "4" },
//   });
// };
// 编辑
const taskUpdata = (row) => {
  router.push({
    path: "/taskManage/manage-add/add",
    query: { taskId: row.taskId, id: row.projectId, name: 2, task: "5" },
  });
};

// 查询
const onSubmit = (val) => {
  val.reviewTime = formatDate(val.reviewTime);
  paramsSubmit = val;
  getTaskListFn();
};
// 重置
const reset = (val) => {
  val = "";
  getTaskListFn();
};
// 切换每页条数
const handleSizeChange = (valueSise) => {
  pageSize.value = valueSise;
  getTaskListFn();
};
// 切换页码
const handleCurrentChange = (valuePage) => {
  pageNum.value = valuePage;
  getTaskListFn();
};

// 删除
const delTask = async (row) => {
  ElMessageBox.confirm("确定删除该项目吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    if (row && row.projectId) {
      const res = await delTaskApi(row.taskId);
      if (res.code === 200) {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        await getTaskListFn();
      }
    }
  });
};

const handeleRevoke = (row) => {
  ElMessageBox.confirm("确定要撤回吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      if (row) {
        const result = await withdrawApi(row);
        if (result.code === 200) {
          // 删除成功的提示
          ElMessage.success(result.msg);
          getTaskListFn();
        }
      }
    })
    .catch((err) => {
      // 用户点击取消执行的操作
      ElMessage.error("取消");
      // console.log(err);
    });
};

onMounted(async () => {
  await getTaskListFn();
  setTimeout(() => {
    loading.value = false;
  }, 500);
});
</script>

<style scoped lang="scss">
.container {
  padding: 20px;
}
</style>
